﻿using System;
using System.Collections.Generic;
using Authority.Logic;
using Authority.Model;
using Common.Library;
using Ioc.Library;
using Microsoft.AspNetCore.Mvc;
using Tool.Model;
using Web.Manage.App_Start;

namespace Web.Manage.Areas.Authority.Controllers
{
    /// <summary>
    /// 用户管理控制器
    /// </summary>
    [Area("Authority")]

    public class UserController : BaseController
    {

        #region 界面
        /// <summary>
        /// 列表界面
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyView)]
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 添加界面
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult Add()
        {
            return View();
        }

        /// <summary>
        /// 编辑界面
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult Modify()
        {
            return View();
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult ChangePwd()
        {
            return View();
        }

        /// <summary>
        /// 角色授权
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult RoleAuthorization()
        {
            return View();
        }
        #endregion

        #region 查询操作
        /// <summary>
        /// 获取管理员分页列表
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult LoadUserList()
        {
            var resultInfo = new ResultJsonInfo<List<View_SysUserEntity>>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<UserInfoQueryRequest>>();
                ISysUserService service = UnityCIContainer.Instance.GetService<ISysUserService>();
                resultInfo = service.LoadSysUserList(parametersInfo);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        /// 获取部门树状列表信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult LoadDepartmentTreeList()
        {
            var resultInfo = new ResultJsonInfo<List<TreeInfo>>();
            try
            {
                //实体接收数据
                ISysDepartmentService service = UnityCIContainer.Instance.GetService<ISysDepartmentService>();
                resultInfo = service.LoadTreeListInfo();
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        /// 获取所有的岗位列表信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult LoadPostList() {

            var resultInfo = new ResultJsonInfo<List<SelectListInfo>>();
            try
            {
                //实体接收数据
                ISysPostService service = UnityCIContainer.Instance.GetService<ISysPostService>();
                resultInfo = service.LoadSysPostAllList();
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        /// 获取所有的部门列表信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult LoadDepartmentList()
        {

            var resultInfo = new ResultJsonInfo<List<SelectListInfo>>();
            try
            {
                //实体接收数据
                ISysDepartmentService service = UnityCIContainer.Instance.GetService<ISysDepartmentService>();
                resultInfo = service.LoadSelectListInfo();
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        /// 获取所有的角色列表信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult LoadRoleList()
        {
            var resultInfo = new ResultJsonInfo<List<SelectListInfo>>();
            try
            {
                //实体接收数据
                ISysRoleService service = UnityCIContainer.Instance.GetService<ISysRoleService>();
                resultInfo = service.LoadSysRoleAllList();
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
            }
            return ReturnContent(resultInfo);
        }
        #endregion

        #region 编辑操作
        /// <summary>
        /// 增加用户信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyAction, ButtonAuthorizationCode.Add)]
        public ActionResult AddInfo()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<UserAddRequest>>();
                ISysUserService service = UnityCIContainer.Instance.GetService<ISysUserService>();
                resultInfo = service.AddInfo(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyAction, ButtonAuthorizationCode.Edit)]
        public ActionResult ModifyInfo()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<UserModifyRequest>>();
                ISysUserService service = UnityCIContainer.Instance.GetService<ISysUserService>();
                resultInfo = service.ModifyInfo(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        /// 修改用户密码
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyAction, ButtonAuthorizationCode.PswEdit)]
        public ActionResult ModifyPswInfo()
        {
            var resultInfo = new ResultJsonInfo<bool>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<UserPswModifyRequest>>();
                ISysUserService service = UnityCIContainer.Instance.GetService<ISysUserService>();
                resultInfo = service.SaveChangePwd(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        #endregion

        #region 删除操作
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyAction, ButtonAuthorizationCode.Remove)]
        public ActionResult Remove()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<List<string>>>();

                ISysUserService service = UnityCIContainer.Instance.GetService<ISysUserService>();

                resultInfo = service.Remove(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }
        #endregion
    }
}